/**************************************************************************
 *
 *                Admin Over IM
 *
 **************************************************************************
 *  <Copyright>
 *    Copyright (C) 2008  Basov Alexander
 *
 *  <Unit> jabber.h
 *    <Purpose>
 *    <Effects>
 *    <Perfomance>
 **************************************************************************
 *  $Revision: 138 $ $Author: coopht $
 *  $Date: 2008-03-27 17:30:31 +0000 (Thu, 27 Mar 2008) $
 **************************************************************************/

#ifndef __JABBER_H_
#define __JABBER_H_


#include <string>
#include <cerrno>
#include <iostream>

#include "iq.h"
#include "iqfactory.h"

using std::string;

enum J_STATUS {AWAY, 
	       CHAT,
	       DND,
	       ONLINE, 
	       UNAVAILABLE,
	       XA};

class Jabber
{
public:
  Jabber () {iqf = new IqFactory (); _authOK = false;};
  void CloseStream ();
  void Connect (const string ip, const int port);
  void idle ();
  void login (const string userName, const string password);
  void OpenStream (const string to);
  void setStatus (const J_STATUS s, const string msg = "");

  /* Public API */
  /*-------------------------------------------------------------*/  
  virtual void onAuthOK () {std::cout << "AUTH OK \n";}
  virtual void onVersionRequest (const string from) {std::cout << "OnVerReq\n";}
  virtual void onServiseDiscoveryRequest (const string id, const string from);
  /*-------------------------------------------------------------*/


  IqFactory* getIqFactory () {return this->iqf;};

  // TODO: must be in a private part
  // private Event Function

  void onIQRecieve (IQ *IQVal);
  

private:  
  int Select(int nfds, 
	     fd_set *readfds, 
	     fd_set *writefds, 
	     fd_set *exceptfds,
	     struct timeval *timeout);  
  void jparse (const string);

  void send (const string data);
  const string toXmlStringStatus (J_STATUS s);
  string recieve (size_t bufSize);
  inline void err (const string msg) 
  {
    std::cerr << msg << " : " << strerror (errno) << std::endl;
    exit (EXIT_FAILURE);
  };
 
private:
  int sockfd;
  bool _authOK;
  IqFactory *iqf;
  
  // Jabber variables
private:
  string hostName;  
};


#endif // __JABBER_H_
